package com.wanmait.myhouse.mediatorController;

import com.wanmait.myhouse.dao.MediatorDAO;
import com.wanmait.myhouse.vo.Mediator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Controller
@RequestMapping("mediator")
public class MediatorRegisterController {
    @GetMapping("register")
    public String register(){
        return "/mediator/register";
    }

    @PostMapping("medRegisterName")
    @ResponseBody
    public String medRegisterName(Mediator mediator){
        MediatorDAO mediatorDAO = new MediatorDAO();
        Mediator mediator1 = mediatorDAO.FindByMediatorName(mediator);
        if (mediator1 == null){
            return "0";
        }else {
            return "1";
        }
    }

    @PostMapping("addMedRegister")
    @ResponseBody
    public String addMedRegister(Mediator mediator){
        MediatorDAO mediatorDAO = new MediatorDAO();
        mediator.setHeadImg("20250103161957.jpg");
        mediator.setBanLogin(true);
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Timestamp time = Timestamp.valueOf(localDateTime.format(formatter));
        mediator.setMediatorRegtime(time);
        mediatorDAO.add(mediator);
        return "1";
    }
}

